// pages/GradeIntegral/GradeIntegral.js
const app= getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      loading: false,
      loadingconflg: false,
      nodata: false,
      startdate: '',
      enddate: '',
      page: 1,//页码
      totalpage: 1,//总页数
      layer: false,
      showtype: "",//遮罩层显示的内容类型,date日期,shop店铺,man介绍人
      iscustom: false,
      searchstr: '',//介绍人查询条件
      manpage: 1,//介绍人查询页面
      datearry: ["", "本日", "本月", "本年", "自定义"],
      shoplist: [],//店铺列表
      manlist: [],//介绍人列表
      manloading: false,
      manloadingconflg: false,
      mannodata: false,
      summary: {
          listdata: [],
          statistic: {
              MoneyAll: 0,
              SendScoreAll: 0,
              SendCashAll: 0,
              SendCardScoreAll: 0
          }//统计数据
      },
      screen: {//筛选条件选中
          dateindex: 1,//选中的时间
          shopid: '',//选中的店铺id
          shopname: '',//选中的店铺名称
          manid: '',//选中的介绍人
          manname: ''//选中的介绍人名称
      },
      param: {
          bDate: '',
          eDate: '',
          introducerId: '',//介绍人id
          eoId: '',//店铺id
          page: 1,//页面
          census: true//是否查询统计数据
      }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var _this = this;
      var time = app.common.getTimeSection(1, "yyyy-MM-dd");
      _this.setData({
          "param.bDate": time.sdate,
          "param.eDate": time.edate
      });
      _this.GetShopList(function(){
          _this.GetList(false);
      });
  },
  //获取列表数据
  GetList: function (isappend) {
      var _this = this;
      var listdata = _this.data.summary.listdata;
      wx.showLoading({
          title: '加载中',
      });
      var param = _this.data.param;
      app.http.POST('/MiniManagerMember/GetGradeIntegralList', param, (err, data) => {
          if (data.code == 1) {
              var list = data.data.List;
              if (list == null || list.length < 1) {
                  this.setData({
                      "summary.listdata": list,
                      "loading": false,
                      "loadingconflg": false,
                      "nodata": true
                  })
              } else {
                  if (isappend) {
                      listdata = listdata.concat(list);
                  } else {
                      listdata = list;
                  }
                  _this.setData({
                      "summary.listdata": listdata,
                      "loading": false,
                      "loadingconflg": false,
                      "nodata": false
                  });

              }
              if (typeof (data.data.TotalPage) !="undefined") {
                  _this.setData({
                      "totalpage": data.data.TotalPage
                  });
              }
              if (typeof (data.data.MoneyAll) != "undefined") {
                  _this.setData({
                      "summary.statistic.MoneyAll": data.data.MoneyAll
                  });
              }
              if (typeof (data.data.SendScoreAll) != "undefined") {
                  _this.setData({
                      "summary.statistic.SendScoreAll": data.data.SendScoreAll
                  });
              }
              if (typeof (data.data.SendCashAll) != "undefined") {
                  _this.setData({
                      "summary.statistic.SendCashAll": data.data.SendCashAll
                  });
              }
              if (typeof (data.data.SendCardScoreAll) != "undefined") {
                  _this.setData({
                      "summary.statistic.SendCardScoreAll": data.data.SendCardScoreAll
                  });
              }
          } else if (data.code == 2) {
              wx.showToast({
                  title: data.msg,
                  icon: 'none',
                  duration: 2000
              })
              _this.setData({
                  "loading": false,
                  "nodata": true
              })
          } else {
              wx.showToast({
                  title: '请求失败',
                  icon: 'none',
                  duration: 2000
              })
              _this.setData({
                  "loading": false,
                  "nodata": true
              })
          }
          wx.hideLoading();
      })
  },
  //获取授权店铺列表
  GetShopList: function (fn) {
      app.http.POST('/MiniManagerMember/GetValidateShopList', {}, (err, data) => {
          if (data.code == 1) {
              if (data.data.length < 1) {
                  wx.showToast({
                      title: "您还未绑定店铺",
                      icon: 'none',
                      duration: 2000,
                      success: function () {
                          wx.navigateBack();
                      }
                  })
              }
              this.setData({
                  "shoplist": data.data,
                  "param.eoId": data.data[0].eoID,
                  "screen.shopid": data.data[0].eoID,
                  "screen.shopname": data.data[0].eoName,
              });
              fn&&fn();
          } else if (data.code == 2) {
              wx.showToast({
                  title: data.msg,
                  icon: 'none',
                  duration: 2000
              })
          }
          else {
              wx.hideLoading();
              wx.showToast({
                  title: '请求失败',
                  icon: 'none',
                  duration: 2000
              })
          }

      });
  },
  lower: function (e) {
      var _this = this;
      var page = _this.data.page + 1;
      if (page <= _this.data.totalpage) {
          _this.setData({
              "page": page,
              "param.page": page,
              "loading": true,
              "loadingconflg": true,
          })
          _this.GetList(true);
      } else {
          _this.setData({
              "loading": true,
              "loadingconflg": false,
          })
      }
  },
  //介绍人翻页
  lowertwo: function (e) {
      var _this = this;
      var page = _this.data.manpage + 1;
      if (page <= _this.data.mantotalpage) {
          _this.setData({
              "manpage": page,
              "manloading": true,
              "manloadingconflg": true,
          })
          _this.GetIntroducerList(true);
      } else {
          _this.setData({
              "manloading": true,
              "manloadingconflg": false,
          })
      }
  },
  //显示弹层
  ShowLayer: function (e) {
      var dataset = e.currentTarget.dataset;
      if (typeof (dataset.layer) != 'undefined' && !dataset.layer) {
          return;
      }
      var _this = this;
      var layer = _this.data.layer;
      var showtype = _this.data.showtype;
      if (showtype == dataset.tp) {
          _this.setData({
              layer: false,
              showtype: ""
          });
      } else {
          _this.setData({
              layer: true,
              showtype: dataset.tp
          });

      }
  },
  //隐藏弹出层
  HideLayer: function (e) {
      var _this = this;
      _this.setData({
          layer: false,
          showtype: ""
      });
  },
  //日期类型筛选
  DateChange: function (e) {
      var dataset = e.currentTarget.dataset;
      var id = parseInt(dataset.id);
      var iscustom = this.data.iscustom;
      this.setData({
          "screen.dateindex": id
      })
      if (id == 4) {//自定义选择时间
          iscustom = true;
          this.setData({
              "iscustom": iscustom,
              "param.bDate": '',
              "param.eDate": ''
          });
      }
      else {
          iscustom = false;
          var datetype = id == 3 ? 4 : id;//本年为4;
          var time = app.common.getTimeSection(datetype, "yyyy-MM-dd");
          this.setData({
              "iscustom": iscustom,
              "param.bDate": time.sdate,
              "param.eDate": time.edate
          })
          this.GetList(false);
      }
      this.HideLayer();
  },
  //开始时间筛选
  startDateChange: function (e) {
      this.setData({
          "param.bDate": e.detail.value,
          "startdate": e.detail.value
      });
  },
  //结束时间筛选
  endDateChange: function (e) {
      this.setData({
          "param.eDate": e.detail.value,
          "enddate": e.detail.value
      });
  },
  //自定义筛选
  CustomScreen: function () {
      var _this = this;
      var startdate = _this.data.startdate;
      var enddate = _this.data.enddate;
      if (startdate == "") {
          wx.showToast({
              title: '请选择开始时间',
              icon: 'none',
              duration: 2000
          })
          return;
      }
      if (enddate == "") {
          wx.showToast({
              title: '请选择结束时间',
              icon: 'none',
              duration: 2000
          })
          return;
      }
      _this.GetList(false);
  },
  //店铺筛选
  ShopChange: function (e) {
      var dataset = e.currentTarget.dataset;
      this.setData({
          "screen.shopid": dataset.id,
          "screen.shopname": dataset.name,
          "param.eoId": dataset.id
      })
  },
  SearchStr: function (e) {
      this.setData({
          searchstr: e.detail.value
      });
  },
  SeachMan: function (e) {
      this.GetIntroducerList(false);
  },
  GetIntroducerList: function (append) {
      var _this = this;
      var searchstr = _this.data.searchstr;
      wx.showLoading({
          title: '加载中',
      });
      // var shoplist = _this.data.shoplist;
      var screen = _this.data.screen;
      var eoid = screen.shopid;
      var page = _this.data.manpage;
      var search = _this.data.searchstr;
      var listdata = _this.data.manlist;
      app.http.POST('/MiniManagerMember/GetIntroducerList', {
          eoId: eoid,
          search: search,
          page: page
      }, (err, data) => {
          if (data.code == 1) {
              var list = data.data.List;
              if (list == null || list.length < 1) {
                  _this.setData({
                      "manlist": list,
                      "manloading": false,
                      "manloadingconflg": false,
                      "mannodata": true
                  })
              } else {
                  if (append) {
                      listdata = listdata.concat(list);
                  } else {
                      listdata = list;
                  }
                  _this.setData({
                      "manlist": listdata,
                      "mantotalpage": data.data.TotalPage,
                      "manloading": false,
                      "manloadingconflg": false,
                      "mannodata": false
                  })
              }

          } else if (data.code == 2) {
              wx.showToast({
                  title: data.msg,
                  icon: 'none',
                  duration: 2000
              })
              _this.setData({
                  "manloading": false,
                  "mannodata": true
              })
          }
          else {
              wx.hideLoading();
              wx.showToast({
                  title: '请求失败',
                  icon: 'none',
                  duration: 2000
              })
              _this.setData({
                  "manloading": false,
                  "mannodata": true
              })
          }
          wx.hideLoading();
      })
  },
  //选择介绍人
  ChangeMan: function (e) {
      var dataset = e.currentTarget.dataset;
      var manid = this.data.screen.manid;
      if (dataset.id == manid) {
          this.setData({
              "screen.manid": "",
              "screen.manname": "",
              "param.introducerId": "",
          })
      } else {
          this.setData({
              "screen.manid": dataset.id,
              "screen.manname": dataset.name,
              "param.introducerId": dataset.id,
          })
      }

      this.GetList(false);
      this.HideLayer();
  },
  ToDetail: function (e) {
      var dataset = e.currentTarget.dataset;
      //传参数到详情页面
      //日期选中类型,店铺选中类型,介绍人选中类型,介绍人选中名称
      //开始时间,结束时间
      var dateindex = this.data.screen.dateindex;
      var shopid = this.data.screen.shopid;
      var shopname = this.data.screen.shopname;
      var manid = this.data.screen.manid;
      var manname = this.data.screen.manname;
      var bdate = this.data.param.bDate;
      var edate = this.data.param.eDate;
      var sdate = dataset.sdate;
      wx.navigateTo({
          url: 'GradeIntegralDetails?dateindex=' + dateindex + '&shopid=' + shopid + '&shopname=' + shopname + '&manid=' + manid + '&manname=' + manname + '&bdate=' + bdate + '&edate=' + edate + '&sdate=' + sdate + '',
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: '积分发放统计'
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})